package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.SelectCarBrandAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CarBrandPersenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CarBrandPersenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarBrandView;
import com.bjesc_b2c.app.appraiset.widget.view.IndexView;
import com.example.codeutils.utils.LogUtils;
import com.orhanobut.logger.Logger;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.SelectCarBrandBean;

/**
 * Created by Jason Chen on 2017/2/22.
 * 选择品牌
 */

public class SelectCarBrandActivity extends BaseActivity implements CarBrandView, MultiItemTypeAdapter.OnItemClickListener, IndexView.OnSetIndexListenre {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.index_view)
    IndexView indexView;
    private int type;
    private CarBrandPersenter carBrandPersenter;
    private SelectCarBrandAdapter brandAdapter;
    private List<SelectCarBrandBean> brandList;
    private LinearLayoutManager manager;
    private String brandId;
    private String brandName;

    @Override
    protected int getLayout() {
        return R.layout.activity_select_car_brand;
    }

    @Override
    protected void initEventAndData() {
        type = getIntent().getIntExtra("Brand",1);
    }

    @Override
    protected void createPresenter() {
        carBrandPersenter = new CarBrandPersenterImpl(this);
        carBrandPersenter.getCarBrand();
    }

    private void initViews() {
        brandAdapter = new SelectCarBrandAdapter(this, R.layout.layout_item_select_car_brand, brandList);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(brandAdapter);
        recyclerView.setOnScrollListener(scrollListener);
        brandAdapter.setOnItemClickListener(this);
        indexView.setOnSetIndexListenre(this);
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = manager.findFirstVisibleItemPosition();
            indexView.refresh(brandList.get(position).getFirstLetter().charAt(0));
        }
    };

    @Override
    public void setIndexListenre(char index) {
        for (int i = 0; i < brandList.size(); i++) {
            if (String.valueOf(index).equals(brandList.get(i).getFirstLetter())) {
                manager.scrollToPositionWithOffset(i, 0);
                break;//加了break显示才会更准确—yd
            }
        }
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        for (int i = 0; i < brandList.size(); i++) {
            if (i == position) {
                brandList.get(i).setCheck(true);
            } else {
                brandList.get(i).setCheck(false);
            }
        }
        if (type < 3) {
            indexView.refresh(brandList.get(position).getFirstLetter().charAt(0));
            BasicInfoActivity.brandId = brandList.get(position).getId();
            BasicInfoActivity.brandName = brandList.get(position).getName();
            brandId = brandList.get(position).getId();
            brandName = brandList.get(position).getName();
            Logger.d("brandId===" + brandList.get(position).getId() + "  " + brandList.get(position).getName());
            Intent intent = new Intent(SelectCarBrandActivity.this, CarSeriesActivity.class);
            intent.putExtra("brandId", brandList.get(position).getId());
            intent.putExtra("Brand",type);
            startIntent(intent, RequestAndResultCode.SelectCarBrandActivity_TO_CarSeriesActivity);
        }else {
            Intent intent = new Intent();
            intent.putExtra("brandId", brandList.get(position).getId());
            intent.putExtra("brandName",brandList.get(position).getName());
            setResult(RequestAndResultCode.CarModelActivity_Result,intent);
            finish();
        }
        brandAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RequestAndResultCode.CarSeriesActivity_Result) {
            Intent intent = new Intent();
            intent.putExtra("brandId", brandId);
            intent.putExtra("brandName",brandName);
            if (type == 2) {
                intent.putExtra("seriesId", data.getStringExtra("seriesId"));
                intent.putExtra("seriesName", data.getStringExtra("seriesName"));
            }
            setResult(RequestAndResultCode.SelectCarBrandActivity_ResultCode, intent);
            Logger.d("onActivityResult");
            finish();
        }
    }

    @Override
    public void resultCarBrand(List<SelectCarBrandBean> brandList) {
        //按字母排序-yd
        Collections.sort(brandList, new Comparator<SelectCarBrandBean>(){
            public int compare(SelectCarBrandBean carBrandBean1, SelectCarBrandBean carBrandBean2) {
                if(carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)){
                    return 1;
                }
                if(carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)){
                    return 0;
                }
                return -1;
            }
        });
        this.brandList = brandList;
        LogUtils.e(brandList.toString());
        initViews();
    }
}
